<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="70px">
      <el-form-item label="项目名称" prop="entryName">
        <el-input
          v-model="queryParams.entryName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目类别" prop="projectCategory">
        <el-select v-model="queryParams.projectCategory" placeholder="请选择项目类别" clearable>
          <el-option
            v-for="dict in dict.type.yqg_project_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="一级分类" prop="projectCategory">
        <el-select v-model="queryParams.firstLevelClassification" placeholder="请选择一级分类" clearable>
          <el-option
            v-for="dict in dict.type.yqg_first_level_classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--
      <el-form-item label="二级分类" prop="secondaryClassification">
        <el-select v-model="queryParams.secondaryClassification" placeholder="请选择二级分类" clearable>
          <el-option
            v-for="dict in dict.type.yqg_secondary_classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类" prop="tertiaryClassification">
        <el-select v-model="queryParams.tertiaryClassification" placeholder="请选择三级分类" clearable>
          <el-option
            v-for="dict in dict.type.yqg_tertiary_classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
-->
      <el-form-item  label="项目代码" prop="projectCode" v-show="showSearch">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch" label="助记码" prop="mnemonicCode">
        <el-input
          v-model="queryParams.mnemonicCode"
          placeholder="请输入助记码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch" label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch" label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch" label="提成价" prop="commissionPrice">
        <el-input
          v-model="queryParams.commissionPrice"
          placeholder="请输入提成价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch" label="上钟时间" prop="clockUpTime">
        <el-input
          v-model="queryParams.clockUpTime"
          placeholder="请输入上钟时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>
    <el-form>

      <div style="text-align: right;margin-right:-9px;">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:project:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" :index="indexMethod" align="center"
                       v-if="columns[0].visible" />

      <el-table-column label="项目名称" align="center" prop="entryName" width="200" v-if="columns[1].visible" />
      <el-table-column label="项目类别" align="center" prop="projectCategory" width="200"  v-if="columns[2].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yqg_project_category" :value="scope.row.projectCategory"/>
        </template>
      </el-table-column>

      <el-table-column label="一级分类" align="center" prop="firstLevelClassification"  width="100" v-if="columns[3].visible" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yqg_first_level_classification" :value="scope.row.firstLevelClassification"/>
        </template>
      </el-table-column>

     <el-table-column label="二级分类" align="center" prop="secondaryClassification"  width="100" v-if="columns[4].visible" >
       <template slot-scope="scope">
         <dict-tag :options="dict.type.yqg_secondary_classification" :value="scope.row.secondaryClassification"/>
       </template>
     </el-table-column>
      <!--<el-table-column label="三级分类" align="center" prop="tertiaryClassification"  width="100" v-if="columns[5].visible">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yqg_tertiary_classification" :value="scope.row.tertiaryClassification"/>
      </template>
    </el-table-column>
    -->
     <el-table-column label="项目代码" align="center" prop="projectCode"  v-if="columns[5].visible" />
     <el-table-column label="助记码" align="center" prop="mnemonicCode"  v-if="columns[6].visible" width="180" />
     <el-table-column label="单位" align="center" prop="unit"  v-if="columns[7].visible" />
     <el-table-column label="单价" align="center" prop="unitPrice"  v-if="columns[8].visible" />
     <el-table-column label="提成价" align="center" prop="commissionPrice"  v-if="columns[9].visible" />
     <el-table-column label="货余量" align="center" prop="cargoAllowance"  />
     <el-table-column label="上钟时间" align="center" prop="clockUpTime"   v-if="columns[10].visible" width="180" />
     <el-table-column label="备注" align="center" prop="note1"  v-if="columns[11].visible" />

     <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
       <template slot-scope="scope">
         <el-button
           size="mini"
           type="text"
           icon="el-icon-edit"
           @click="handleUpdate(scope.row)"
         >修改</el-button>
         <el-button
           size="mini"
           type="text"
           icon="el-icon-delete"
           @click="handleDelete(scope.row)"
         >删除</el-button>

       </template>
     </el-table-column>
   </el-table>

   <pagination
     v-show="total>0"
     :total="total"
     :page.sync="queryParams.pageNum"
     :limit.sync="queryParams.pageSize"
     @pagination="getList"
   />

   <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="755px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px":inline="true">
        <el-form-item label="项目名称" prop="entryName">
          <el-input v-model="form.entryName" placeholder="请输入项目名称" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="项目类别" prop="projectCategory">
          <el-select v-model="form.projectCategory" placeholder="请选择项目类别" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_project_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="一级分类" prop="firstLevelClassification">
          <el-select v-model="form.firstLevelClassification" placeholder="请选择一级分类" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_first_level_classification"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" prop="secondaryClassification">
          <el-select v-model="form.secondaryClassification" placeholder="请选择二级分类" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_secondary_classification"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--  <el-form-item label="三级分类" prop="tertiaryClassification">
          <el-select v-model="form.tertiaryClassification" placeholder="请选择三级分类" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_tertiary_classification"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
  -->
        <el-form-item label="项目代码" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目代码" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="助记码" prop="mnemonicCode">
          <el-input v-model="form.mnemonicCode" placeholder="请输入助记码"style="width: 240px" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价"style="width: 240px" />
        </el-form-item>
        <el-form-item label="提成价" prop="commissionPrice">
          <el-input v-model="form.commissionPrice" placeholder="请输入提成价"style="width: 240px" />
        </el-form-item>
        <el-form-item label="上钟时间" prop="clockUpTime">
          <el-input v-model="form.clockUpTime" placeholder="请输入上钟时间"style="width: 240px" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/module/wyw/project";

export default {
  name: "Project",
  dicts: ['yqg_project_category','yqg_first_level_classification','yqg_secondary_classification','yqg_tertiary_classification'],
  data() {
    return {
      // 遮罩层
      columns: [
        {key: 0, label: `序号`, visible: true},
        {key: 1, label: `项目名称`, visible: true},
        {key: 2, label: `项目类别`, visible: true},
        {key: 3, label: `一级分类`, visible: false},
        {key: 4, label: `二级分类`, visible: false},
        {key: 5, label: `项目代码`, visible:true},
        {key: 6, label: `助记码`, visible: true},
        {key: 7, label: `单位`, visible: true},
        {key: 8, label: `单价`, visible: true},
        {key: 9, label: `提成价`, visible: true},
        {key: 10, label: `上钟时间`, visible: false},
        {key: 11, label: `备注`, visible: false},
      ],
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entryName: null,
        projectCategory: null,
        firstLevelClassification: null,
        secondaryClassification: null,
        tertiaryClassification: null,
        projectCode: null,
        mnemonicCode: null,
        cargoAllowance:null,
        unit: null,
        unitPrice: null,
        commissionPrice: null,
        clockUpTime: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: null,
        entryName: null,
        projectCategory: null,
        firstLevelClassification: null,
        secondaryClassification: null,
        tertiaryClassification: null,
        projectCode: null,
        mnemonicCode: null,
        unit: null,
        unitPrice: null,
        commissionPrice: null,
        clockUpTime: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id| this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    indexMethod(index) {
      index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return index
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目信息编号为"' + ids + '"的数据项？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/project/export', {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
